package p100;

import java.util.Arrays;

/**
 * @author pengjz <br>
 * @version 1.0 <br>
 * @description Leetcode_131 <br>
 * @date 2021/3/7 22:36 <br>
 */
public class Leetcode_132 {


    public int minCut(String s) {
        int n = s.length();
        boolean dp[][];
        // 构建dp数组
        dp = new boolean[n][n];
        for (boolean[] booleans : dp) {
            Arrays.fill(booleans, true);
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                dp[i][j] = (s.charAt(i) == s.charAt(j)) && dp[i + 1][j - 1];
            }
        }
        int[] res = new int[n];
        Arrays.fill(res, Integer.MAX_VALUE);
        for (int i = 0; i < n; i++) {
            // 如果0到i本身就是一个回文字符串，切割次数就是0
            if (dp[0][i]) {
                res[i] = 0;
            } else {
                for (int j = 0; j < i; j++) {
                    if (dp[j + 1][i]) {
                        res[i] = Math.min(res[i], res[j] + 1);
                    }
                }
            }
        }
        return res[n - 1];
    }

    public static void main(String[] args) {
        Leetcode_132 l = new Leetcode_132();
        System.out.println(l.minCut("aab"));
    }
}
